package com.hwhhhh.mygrad;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hwhhhh.mygrad.adapter.SearchHistoryAdapter;
import com.hwhhhh.mygrad.utils.DBHelper;
import com.hwhhhh.mygrad.view.IconfontView;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseActivity implements View.OnClickListener{
    private EditText etSearch;
    private IconfontView icBack;
    private TextView tvSearch;
    private List<String> searchHistory;
    private DBHelper dbHelper;
    private RecyclerView rvHistory;

    @Override
    protected void initView() {
        etSearch = findViewById(R.id.et_search);
        icBack = findViewById(R.id.ic_search_back);
        tvSearch = findViewById(R.id.tv_search);
        rvHistory = findViewById(R.id.rv_search_history);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initData() {
        etSearch.requestFocus();// 获取焦点
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);//设置输入盘可见
        icBack.setOnClickListener(this);
        tvSearch.setOnClickListener(this);
        queryData();
        SearchHistoryAdapter searchHistoryAdapter = new SearchHistoryAdapter(searchHistory);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        rvHistory.setAdapter(searchHistoryAdapter);
        rvHistory.setLayoutManager(layoutManager);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ic_search_back) {
            finish();
        } else if (id == R.id.tv_search) {
            String str = etSearch.getText().toString().trim();
            long res = insertData(str);
            Toast.makeText(this, "按了" + res, Toast.LENGTH_SHORT).show();
        }
    }

    private void queryData() {
        dbHelper = new DBHelper(this, "hwh_db", null, 1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("search_history", new String[]{"str"}, null, null, null, null, null);
        if (searchHistory == null) searchHistory = new ArrayList<>();
        while (cursor.moveToNext()) {
            searchHistory.add(0, cursor.getString(cursor.getColumnIndex("str")));
        }
        cursor.close();
    }

    private long insertData(String str) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("str", str);
        return db.insert("search_history", null, values);
    }
}